using UnityEngine;

public class missilemove : BulletController
{
	public bool ishaveaim;

	public Fri_AIController targetai_fri;

	private float maxspeed = 400f;

	private float accspeed = 90f;

	public TrailRenderer tralline;

	public LayerMask layerMask;

	private Vector3 previousPosition;

	private Vector3 movedir;

	private float movedis;

	private RaycastHit hitInfo;

	private AIController curhitAI;

	private Fri_AIController fri_curhitai;

	private float minCheckDis = 1f;

	private Effectobj tempeffectobj;

	private void Update()
	{
		if (!isshot)
		{
			return;
		}
		if (movespeed < maxspeed)
		{
			if (accspeed < 90f)
			{
				accspeed += 40f * Time.deltaTime;
			}
			movespeed += accspeed * Time.deltaTime;
		}
		if (tralline != null && (double)tralline.time < 0.7)
		{
			tralline.time += Time.deltaTime * 0.35f;
		}
		m_transform.Translate(0f, 0f, movespeed * Time.deltaTime, Space.Self);
		if (isplayerbullet)
		{
			if (targetAI != null)
			{
				m_transform.rotation = Quaternion.Lerp(m_transform.rotation, Quaternion.LookRotation(targetAI.bodyself.position - m_transform.position), Time.deltaTime * 15f);
				Vector3 vector = m_transform.InverseTransformPoint(targetAI.bodyself.position);
				if (vector.z < 5f)
				{
					if (ishaveaim)
					{
						targetAI.decaimMissile(this);
					}
					targetAI = null;
				}
			}
		}
		else if (targetTra != null)
		{
			m_transform.rotation = Quaternion.Lerp(m_transform.rotation, Quaternion.LookRotation(targetTra.transform.position - m_transform.position), Time.deltaTime * 15f);
			Vector3 vector2 = m_transform.InverseTransformPoint(targetTra.position);
			if (vector2.z < 5f)
			{
				if (ishaveaim)
				{
					if (targetai_fri != null)
					{
						targetai_fri.decaimMissile(this);
					}
					else if (targetAI != null)
					{
						targetAI.decaimMissile(this);
					}
					else
					{
						plane_smothfollow.instance.decaimMissile(this);
					}
				}
				targetTra = null;
			}
		}
		shotouttime += Time.deltaTime;
		if (!(shotouttime >= (float)limitedtime))
		{
			return;
		}
		isshot = false;
		shotouttime = 0f;
		if (isplayerbullet)
		{
			if (ishaveaim)
			{
				targetAI.decaimMissile(this);
			}
			if (tralline != null)
			{
				tralline.enabled = false;
			}
			missilepool.instance.RecycleMissile(this);
		}
		else
		{
			if (ishaveaim)
			{
				if (targetai_fri != null)
				{
					targetai_fri.decaimMissile(this);
				}
				else if (targetAI != null)
				{
					targetAI.decaimMissile(this);
				}
				else
				{
					plane_smothfollow.instance.decaimMissile(this);
				}
			}
			NPCmissilepool.instance.RecycleMissile(this);
		}
		targetAI = null;
		targetTra = null;
		targetai_fri = null;
		tempeffectobj = EffectPool.instance.GetEffectObj(EFFECTTYPE.missileExplosion);
		if (tempeffectobj != null)
		{
			tempeffectobj.transform.position = m_transform.position;
			tempeffectobj.showeffect();
		}
	}

	public void shot(int buldam, int penetraVal, int parentnum, float startspeed)
	{
		movespeed = startspeed;
		m_transform = base.transform;
		parent = parentnum;
		previousPosition = m_transform.position;
		hitDamage = buldam;
		penetrationVal = penetraVal;
		isshot = true;
		shotouttime = 0f;
		accspeed = 50f;
		UnityVersionUtil.SetActiveRecursive(m_transform.gameObject, state: true);
		if (tralline != null && GlobalInf.CurrentQuality >= Quality.Low)
		{
			tralline.enabled = true;
			tralline.time = 0f;
		}
		if (isplayerbullet)
		{
			if (targetAI != null && targetAI.ai_type == AI_TYPE.aiplane)
			{
				ishaveaim = true;
				targetAI.addmissile(this);
				targetAI.ismissileaim = true;
			}
		}
		else if (targetai_fri != null)
		{
			if (targetai_fri.fri_type == AI_TYPE.aiplane)
			{
				ishaveaim = true;
				targetai_fri.addmissile(this);
				targetai_fri.ismissileaim = true;
			}
		}
		else if (targetAI != null)
		{
			ishaveaim = true;
			targetAI.addmissile(this);
			targetAI.ismissileaim = true;
		}
		else
		{
			ishaveaim = true;
			plane_smothfollow.instance.addaimMissile(this);
			UIController.instance.planeisaim(istrue: true);
		}
	}

	private void FixedUpdate()
	{
		if (!isshot)
		{
			return;
		}
		movedis = Vector3.Distance(previousPosition, m_transform.position);
		if (!(movedis >= minCheckDis))
		{
			return;
		}
		movedir = (m_transform.position - previousPosition).normalized;
		if (Physics.Raycast(previousPosition, movedir, out hitInfo, movedis, layerMask.value))
		{
			tempeffectobj = null;
			if (isplayerbullet)
			{
				switch (hitInfo.collider.gameObject.tag)
				{
				case "AIPlane":
					curhitAI = hitInfo.transform.GetComponent<AI_plane>();
					if (curhitAI != null)
					{
						curhitAI.GetDamage(hitDamage, penetrationVal, parent, isbullet: false);
					}
					break;
				case "ship":
					curhitAI = hitInfo.transform.parent.GetComponent<ShipController>();
					if (curhitAI != null)
					{
						curhitAI.GetDamage(hitDamage, penetrationVal, parent, isbullet: false);
					}
					break;
				case "buildingai":
					curhitAI = hitInfo.transform.parent.GetComponent<BuildingAIController>();
					if (curhitAI != null)
					{
						curhitAI.GetDamage(hitDamage, penetrationVal, parent, isbullet: false);
					}
					break;
				}
				if (InviewPos(hitInfo.point))
				{
					tempeffectobj = EffectPool.instance.GetEffectObj(EFFECTTYPE.missileExplosion);
					if (tempeffectobj != null)
					{
						tempeffectobj.transform.position = hitInfo.point;
						tempeffectobj.showeffect();
					}
				}
				isshot = false;
				shotouttime = 0f;
				if (ishaveaim && targetAI != null)
				{
					targetAI.decaimMissile(this);
				}
				targetAI = null;
				targetTra = null;
				targetai_fri = null;
				if (tralline != null)
				{
					tralline.enabled = false;
				}
				missilepool.instance.RecycleMissile(this);
			}
			else
			{
				switch (hitInfo.collider.gameObject.tag)
				{
				case "Player":
					plane_smothfollow.instance.getdamage(hitDamage, penetrationVal, parent, isbullet: false);
					break;
				case "friplane":
					fri_curhitai = hitInfo.transform.GetComponent<Fri_plane>();
					if (fri_curhitai != null)
					{
						fri_curhitai.GetDamage(hitDamage, penetrationVal, parent);
					}
					break;
				case "friship":
					fri_curhitai = hitInfo.transform.parent.GetComponent<Fri_ship>();
					if (fri_curhitai != null)
					{
						fri_curhitai.GetDamage(hitDamage, penetrationVal, parent);
					}
					break;
				case "fribuilding":
					fri_curhitai = hitInfo.transform.parent.GetComponent<Fri_building>();
					if (fri_curhitai != null)
					{
						fri_curhitai.GetDamage(hitDamage, penetrationVal, parent);
					}
					break;
				case "AIPlane":
					if (GlobalInf.isInternalInjury)
					{
						curhitAI = hitInfo.transform.GetComponent<AI_plane>();
						if (curhitAI != null)
						{
							curhitAI.GetDamage(hitDamage, penetrationVal, parent, isbullet: false);
						}
					}
					break;
				}
				if (InviewPos(hitInfo.point))
				{
					tempeffectobj = EffectPool.instance.GetEffectObj(EFFECTTYPE.missileExplosion);
					if (tempeffectobj != null)
					{
						tempeffectobj.transform.position = hitInfo.point;
						tempeffectobj.showeffect();
					}
				}
				isshot = false;
				shotouttime = 0f;
				if (ishaveaim)
				{
					if (targetai_fri != null)
					{
						targetai_fri.decaimMissile(this);
					}
					else if (targetAI != null)
					{
						targetAI.decaimMissile(this);
					}
					else
					{
						plane_smothfollow.instance.decaimMissile(this);
					}
				}
				targetAI = null;
				targetTra = null;
				targetai_fri = null;
				NPCmissilepool.instance.RecycleMissile(this);
			}
		}
		previousPosition = m_transform.position;
	}
}
